# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User

import datetime

# Create your models here.

class Category(models.Model):
    pid = models.IntegerField(default=0)
    oid = models.IntegerField(default=0)
    name = models.CharField(max_length=20)
    
    class Meta:
        ordering = ['id']

    def __unicode__(self):
        return self.name
    
    def get_categorys(self):
        return Category.objects.all()

class Video(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=30)
    image = models.CharField(max_length=20, null=True, blank=True)
    director = models.CharField(max_length=50, null=True, blank=True)
    actor = models.CharField(max_length=100, null=True, blank=True)
    intro = models.TextField(null=True, blank=True)
    area = models.CharField(max_length=20, null=True, blank=True)
    date = models.CharField(max_length=20, null=True, blank=True)
    serial = models.CharField(max_length=20, null=True, blank=True)
    end = models.IntegerField(default=1)
    status = models.IntegerField(default=1)
    update_time = models.DateTimeField()
    hits = models.IntegerField(default=0)
    
    class Meta:
        ordering = ['-update_time']

    def __unicode__(self):
        return self.title
    
    def update_hits(self, video_id):
        video = Video.objects.get(pk=video_id)
        video.hits += 1
        video.save()
        
    def get_lastest_videos(self, category_id, max_num=20):
        return Video.objects.filter(category__id=category_id)[:max_num]
    
    def get_hots_by_hits(self, category_id, max_num=20):
        return Video.objects.filter(category__id=category_id).order_by('-hits')[:max_num]

class Url(models.Model):
    video = models.ForeignKey(Video)
    oid = models.IntegerField(default=0)
    url_type = models.CharField(max_length=10)
    url_name = models.CharField(max_length=10, null=True, blank=True)
    url = models.CharField(max_length=200)
    
    class Meta:
        ordering = ['-oid']

    def __unicode__(self):
        return self.url
    
    def add_url(self, video_id, url_type, url, url_name, oid=0):
        video = Video.objects.get(pk=video_id)
        url = Url(oid=oid, video=video, url_type=url_type, url=url, url_name=url_name)
        url.save()
        video.url_name = url_name
        video.save()
        
        if not video.end:
            follows = Follow.objects.all()
            for follow in follows:
                if follow.video.id == video.id:
                    message = Message()
                    message.add_message(follow.user.id, follow.video.id, url.id)
    
    def get_urls(self, video_id):
        return Url.objects.filter(video__id=video_id)

class Account(models.Model):
    user = models.OneToOneField(User)
    head = models.ImageField(upload_to='film/static/image/head', null=True, blank=True)
    gender = models.IntegerField(default=0)

    def __unicode__(self):
        return self.user.username

class Comment(models.Model):
    video = models.ForeignKey(Video)
    user = models.ForeignKey(User)
    pid = models.IntegerField(default=0)
    publish_time = models.DateTimeField()
    content = models.TextField()
    support = models.IntegerField(default=0)
    against = models.IntegerField(default=0)
    
    class Meta:
        ordering = ['-publish_time']

    def __unicode__(self):
        return self.content
    
    def add_comment(self, video_id, user_id, content, pid=0):
        comment = Comment()
        comment.video = Video.objects.get(pk=video_id)
        comment.user = User.objects.get(pk=user_id)
        comment.pid = pid
        comment.publish_time = datetime.datetime.now()
        comment.content = content
        comment.save()
        
    def update_comment(self, comment_id, support=0, against=0):
        comment = Comment.objects.get(pk=comment_id)
        comment.support += support
        comment.against += against
        comment.save()
        
    def get_comments(self, video_id):
        return Comment.objects.filter(video__id=video_id)

class Problem(models.Model):
    pid = models.IntegerField(default=0)
    user_name = models.CharField(max_length=20)
    publish_time = models.DateTimeField()
    content = models.TextField()
    problem_type = models.CharField(max_length=20)
    reply = models.BooleanField(default=False)
    
    class Meta:
        ordering = ['-publish_time']

    def __unicode__(self):
        return self.content
    
    def add_problem(self, pid, user_name, content, problem_type):
        problem = Problem(pid=pid,
                          user_name=user_name,
                          publish_time=datetime.datetime.now(),
                          content=content,
                          problem_type=problem_type)
        problem.save()
        
    def update_reply(self, problem_id):
        problem = Problem.objects.get(pk=problem_id)
        problem.reply = True
        problem.save()
    
    def get_problems(self):
        return Problem.objects.all()

class Search(models.Model):
    keywords = models.CharField(max_length=30)
    result = models.IntegerField(default=0)
    search_time = models.DateTimeField()

    def __unicode__(self):
        return self.keywords

class History(models.Model):
    user = models.ForeignKey(User)
    video = models.ForeignKey(Video)
    url = models.ForeignKey(Url)
    view_time = models.DateTimeField()

    def __unicode__(self):
        return self.video.title
    
    def add_history(self, user_id, video_id, url_id):
        history = History()
        history.user = User.objects.get(pk=user_id)
        history.video = Video.objects.get(pk=video_id)
        history.url = Url.objects.get(pk=url_id)
        history.view_time = datetime.datetime.now()
        
        historys = History.objects.filter(user__id=user_id)
        for item in historys:
            if item.video.id == history.video.id:
                item.url = history.url
                item.view_time = datetime.datetime.now()
                item.save()
            else:
                history.save()
        
    def del_history(self, history_id):
        history = History.objects.get(pk=history_id)
        history.delete()
        
    def get_historys(self, user_id, max_num=10):
        return History.objects.filter(user__id=user_id)[:max_num]

class Follow(models.Model):
    user = models.ForeignKey(User)
    video = models.ForeignKey(Video)

    def __unicode__(self):
        return self.video.title
    
    def add_follow(self, user_id, video_id):
        follow = Follow()
        follow.user = User.objects.get(pk=user_id)
        follow.video = Video.objects.get(pk=video_id)
        follow.save()
        
    def del_follow(self, follow_id):
        follow = Follow.objects.get(pk=follow_id)
        follow.delete()
        
    def get_follows(self, user_id):
        return Follow.objects.filter(user__id=user_id)

class Message(models.Model):
    user = models.ForeignKey(User)
    video = models.ForeignKey(Video)
    url = models.ForeignKey(Url)
    content = models.CharField(max_length=200)

    def __unicode__(self):
        return self.content
    
    def add_message(self, user_id, video_id, url_id):
        message = Message()
        message.user = User.objects.get(pk=user_id)
        message.video = Video.objects.get(pk=video_id)
        message.url = Url.objects.get(pk=url_id)
        
        messages = Message.objects.filter(user__id=user_id)
        for item in messages:
            if item.video.id == video_id:
                item.url = message.url
                item.content = "您追的  " + item.video.title + "  已经更新到  " + message.video.serial
                item.save()
            else:
                message.content = "您追的  " + message.video.title + "  已经更新到  " + message.video.serial
                message.save()
                
    def del_message(self, message_id):
        message = Message.objects.get(pk=message_id)
        message.delete()
        
    def get_messages(self, user_id):
        return Message.objects.filter(user__id=user_id)
    
    
    
    
